package com.link.base.wechat.basic.util;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * 消息工具类
 *
 * @author ouyangzhenqiang
 * @date 2019/1/2
 */
public class MsgUtil {
    /**
     * 请求消息类型：文本
     */
    public static final String MESSAGE_TYPE_TEXT = "text";

    /**
     * 请求消息类型：图片
     */
    public static final String MESSAGE_TYPE_IMAGE = "image";

    /**
     * 请求消息类型：语音
     */
    public static final String MESSAGE_TYPE_VOICE = "voice";

    /**
     * 请求消息类型：视频
     */
    public static final String MESSAGE_TYPE_VEIDO = "video";

    /**
     * 请求消息类型：地理位置
     */
    public static final String MESSAGE_TYPE_LOCATION = "location";

    /**
     * 请求消息类型：链接
     */
    public static final String MESSAGE_TYPE_LINK = "link";

    /**
     * 请求事件
     */
    public static final String EVENT_TYPE = "event";

    /**
     * 事件类型：成员关注
     */
    public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";

    /**
     * 事件类型：成员取消关注
     */
    public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";

    /**
     * 事件类型：进入应用
     */
    public static final String EVENT_TYPE_ENTER_AGENT = "enter_agent";

    /**
     * 事件类型：上报地理位置
     */
    public static final String EVENT_TYPE_LOCATION = "LOCATION";

    /**
     * 事件类型：异步任务完成事件
     */
    public static final String EVENT_TYPE_BATCH_JOB_RESULT = "batch_job_result";

    /**
     * 事件类型：菜单点击事件
     */
    public static final String EVENT_TYPE_CLICK = "CLICK";

    /**
     * 事件类型：点击菜单链接跳转事件
     */
    public static final String EVENT_TYPE_VIEW = "view";

    /**
     * 事件类型：SCAN(扫描带参数二维码事件)<br />
     * 用户扫描带场景值二维码时，可能推送以下两种事件：<br />
     * 1、如果用户还未关注公众号，则用户可以关注公众号，关注后微信会将带场景值关注事件推送给开发者。<br />
     * 2、如果用户已经关注公众号，则微信会将带场景值扫描事件推送给开发者。<br />
     */
    public static final String EVENT_TYPE_SCAN = "SCAN";

    /**
     * 事件类型：扫码推事件
     */
    public static final String EVENT_TYPE_SCAN_CODE_PUSH = "scancode_push";

    /**
     * 事件类型：扫码推事件且弹出“消息接收中”提示框
     */
    public static final String EVENT_TYPE_SCAN_CODE_WAIT_MSG = "scancode_waitmsg";

    /**
     * 事件类型：弹出系统拍照发图事件
     */
    public static final String EVENT_TYPE_PIC_SYSPHOTO = "pic_sysphoto";

    /**
     * 事件类型：弹出拍照或者相册发图事件
     */
    public static final String EVENT_TYPE_PIC_PHOTO_OR_ALBUM = "pic_photo_or_album";

    /**
     * 事件类型：弹出微信相册发图器事件
     */
    public static final String EVENT_TYPE_PIC_WEIXIN = "pic_weixin";

    /**
     * 弹出地理位置选择器事件
     */
    public static final String EVENT_TYPE_LOCATION_SELECT = "location_select";

    /**
     * 用户领取卡券
     */
    public static final String EVENT_TYPE_USER_GET_CARD = "user_get_card";

    /**
     * request文件流转为String
     *
     * @author ouyangzhenqiang
     * @date 2019/1/2
     */
    public static String getRequestStr(HttpServletRequest request) throws IOException {
        StringBuilder sb = new StringBuilder();
        try (InputStreamReader isr = new InputStreamReader(request.getInputStream(), "UTF-8")) {
            try (BufferedReader in = new BufferedReader(isr)) {
                String line = null;
                while ((line = in.readLine()) != null) {
                    sb.append(line);
                }
                in.close();
            }
            isr.close();
        }
        return sb.toString();
    }

    /**
     * 解析xml文档*
     *
     * @author Lizongting
     * @date 2019/2/26
     */
    public static Element resolveXml(String xmlString) throws Exception {
        SAXReader reader = new SAXReader();
        StringReader sr = new StringReader(xmlString);
        Document document = reader.read(sr);
        return document.getRootElement();
    }

}
